package com.sims.daoImp;

import java.util.List;

import javax.management.Query;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.AttendanceRecord;
import com.sims.dao.IAttendanceRecordDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * AttendanceRecord entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sims.bean.AttendanceRecord
 * @author MyEclipse Persistence Tools
 */

public class AttendanceRecordDAO extends HibernateDaoSupport implements IAttendanceRecordDAO {
	private static final Logger log = LoggerFactory
			.getLogger(AttendanceRecordDAO.class);
	// property constants
	public static final String SID = "sid";
	public static final String COURSE = "course";
	public static final String REASON = "reason";
	public static final String STIME = "stime";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#save(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public void save(AttendanceRecord transientInstance) {
		log.debug("saving AttendanceRecord instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#delete(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public void delete(AttendanceRecord persistentInstance) {
		log.debug("deleting AttendanceRecord instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findById(java.lang.Integer)
	 */
	@Override
	public AttendanceRecord findById(java.lang.Integer id) {
		log.debug("getting AttendanceRecord instance with id: " + id);
		try {
			AttendanceRecord instance = (AttendanceRecord) getHibernateTemplate()
					.get("com.sims.bean.AttendanceRecord", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findByExample(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public List findByExample(AttendanceRecord instance) {
		log.debug("finding AttendanceRecord instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding AttendanceRecord instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from AttendanceRecord as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findBySid(java.lang.Object)
	 */
	@Override
	public List findBySid(Object sid) {
		return findByProperty(SID, sid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findByCourse(java.lang.Object)
	 */
	@Override
	public List findByCourse(Object course) {
		return findByProperty(COURSE, course);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findByReason(java.lang.Object)
	 */
	@Override
	public List findByReason(Object reason) {
		return findByProperty(REASON, reason);
	}

	public List findByTime(Object stime) {
		return findByProperty(STIME, stime);
	}
	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all AttendanceRecord instances");
		try {
			String queryString = "from AttendanceRecord";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#merge(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public AttendanceRecord merge(AttendanceRecord detachedInstance) {
		log.debug("merging AttendanceRecord instance");
		try {
			AttendanceRecord result = getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#attachDirty(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public void attachDirty(AttendanceRecord instance) {
		log.debug("attaching dirty AttendanceRecord instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IAttendanceRecord#attachClean(com.sims.bean.AttendanceRecord)
	 */
	@Override
	public void attachClean(AttendanceRecord instance) {
		log.debug("attaching clean AttendanceRecord instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IAttendanceRecordDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (IAttendanceRecordDAO) ctx.getBean("AttendanceRecordDAO");
	}
	
	public List findStudentAttendanceRecordBySid(Object sid) {
		String queryString = "select new com.sims.beanEnhanced.StudentAttendanceRecord(s,ar) " 
				+ "from Student s, AttendanceRecord ar where "
				+ "s.sid = :ssid ";
		org.hibernate.Query query = getSession().createQuery(queryString);
		query.setInteger("ssid", (Integer) sid);
		return query.list();
	}
}